<script setup>
import {get, logout} from '@/net'
import router from "@/router";
import {useStore} from "@/store";
import {reactive, ref} from "vue";
import {
  Back, Bell,
  ChatDotSquare, Check,
  CreditCard, Document,
  Location,
  MagicStick,
  Magnet, Medal, Message, Money, More, Operation, Postcard, Search, Setting,
  Ship,
  Shop, Star,
  SwitchFilled,
  Trophy, User
} from "@element-plus/icons-vue";
import LightCard from "@/components/LightCard.vue";

const store = useStore()
//加载状态
const loading = ref(true)

//搜索框
const searchInput = reactive({
  type: '1',
  text: '',
})

//刷新页面，加载用户信息
get("/api/user/info",(data)=>{
  // console.log(data)
  //保存到公共区域
  store.user = data;
  //加载完成
  loading.value = false;
})

const notification = ref([])

const loadNotification =
    () => get('/api/notification/list', data => notification.value = data)
loadNotification()

function userLogout() {
  logout(() => router.push("/"))
}

function confirmNotification(id, url) {
  get(`/api/notification/delete?id=${id}`, () => {
    loadNotification()
    window.open(url)
  })
}

function deleteAllNotification() {
  get(`/api/notification/delete-all`, loadNotification)
}
</script>
<template>
  <div class="main-content" v-loading="loading" element-loading-text="正在进入，请稍后...">
    <el-container style="height: 100%" v-if="!loading">
      <el-header class="main-content-header">
        <svg t="1706610416541" class="logo" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1628" width="200" height="200">
          <path d="M192 533.546667L512 715.946667V346.197333z" fill="#3A6A97" p-id="1629"></path>
          <path d="M832 533.546667L512 715.946667V346.197333z" fill="#103F69" p-id="1630"></path>
          <path d="M832 579.157333L512 761.514667v230.485333z" fill="#365D80" p-id="1631"></path>
          <path d="M192 579.157333l320 182.357334v230.485333zM192 533.546667L512 32v373.930667z" fill="#88A2C0" p-id="1632" data-spm-anchor-id="a313x.search_index.0.i1.53b63a81SqttEQ">
          </path><path d="M832 533.546667L512 32v373.930667z" fill="#36719F" p-id="1633"></path>
        </svg>
        <p style="font-size: 25px;">NFT Community</p>
        <div style="flex: 1;padding: 0 20px;text-align: center">
          <el-input v-model="searchInput.text" style="width: 100%;max-width: 450px;"
          placeholder="search for wt u want here...">
            <template #prefix>
              <el-icon><search/></el-icon>
            </template>
            <template #append>
              <el-select style="width: 100px" v-model="searchInput.type">
                <el-option value="1" label="帖子"></el-option>
                <el-option value="2" label="藏品"></el-option>
                <el-option value="3" label="用户"></el-option>
                <el-option value="4" label="账户"></el-option>
              </el-select>
            </template>
          </el-input>
        </div>
        <div class="user-info">

          <el-popover placement="bottom" :width="350" trigger="click">
            <template #reference>
              <el-badge style="margin-right: 15px" is-dot :hidden="!notification.length">
                <div class="notification">
                  <el-icon><Bell/></el-icon>
                  <div style="font-size: 10px">消息</div>
                </div>
              </el-badge>
            </template>
            <el-empty :image-size="80" description="暂时没有未读消息哦~" v-if="!notification.length"/>
            <el-scrollbar :max-height="500" v-else>
              <light-card v-for="item in notification" class="notification-item"
                          @click="confirmNotification(item.id, item.url)">
                <div>
                  <el-tag size="small" :type="item.type">消息</el-tag>&nbsp;
                  <span style="font-weight: bold">{{item.title}}</span>
                </div>
                <el-divider style="margin: 7px 0 3px 0"/>
                <div style="font-size: 13px;color: grey">
                  {{item.content}}
                </div>
              </light-card>
            </el-scrollbar>
            <div style="margin-top: 10px">
              <el-button size="small" type="info" :icon="Check" @click="deleteAllNotification"
                         style="width: 100%" plain>清除全部未读消息</el-button>
            </div>
          </el-popover>
          <div class="profile">
              <div>{{ store.user.username }}</div>
              <div>{{store.user.email}}</div>
          </div>
          <el-dropdown>

            <el-avatar :src="store.avatarUrl"/>
            <template #dropdown>
              <el-dropdown-item>
                <el-icon>
                  <Operation/>
                </el-icon>
                个人设置
              </el-dropdown-item>
              <el-dropdown-item>
                <el-icon>
                  <Message/>
                </el-icon>
                消息列表
              </el-dropdown-item>
              <el-dropdown-item @click="userLogout" divided>
                <el-icon>
                  <Back/>
                </el-icon>
                退出登录
              </el-dropdown-item>
            </template>
          </el-dropdown>
        </div>
      </el-header>
<!--      右边导航栏-->
      <el-container>
        <el-aside width="230px">
          <el-scrollbar style="height: calc(100vh - 55px)">
            <el-menu
                router
                :default-active="$route.path"
                :default-openeds="['1', '2', '3','4']"
                style="min-height: calc(100vh - 55px)">
<!--              论坛-->
              <el-sub-menu index="1">
                <template #title>
                  <el-icon><location/></el-icon>
                  NFT交流论坛
                </template>
                <el-menu-item index="/index">
                  <el-icon><chat-dot-square/></el-icon>
                  <template #title>
                    帖子广场
                  </template>
                </el-menu-item>
                <el-menu-item index="1-2">
                  <el-icon><Magnet /></el-icon>
                  <template #title>
                    优质资源
                  </template>
                </el-menu-item>
                <el-menu-item index="1-3">
                  <el-icon><Trophy /></el-icon>
                  <template #title>
                    热门活动
                  </template>
                </el-menu-item>
                <el-menu-item index="1-4">
                  <el-icon><Ship /></el-icon>
                  <template #title>
                    链上资讯
                  </template>
                </el-menu-item>
                <el-menu-item index="1-5">
                  <el-icon><Shop /></el-icon>
                  <template #title>
                    狗狗考研
                    <el-tag style="margin-left: 10px" size="small" >商业合作</el-tag>
                  </template>
                </el-menu-item>
              </el-sub-menu>
<!--              nft交易市场-->
              <el-sub-menu index="2">
                <template #title>
                  <el-icon><CreditCard /></el-icon>
                  NFT交易市场
                </template>
                <el-menu-item index="/index/nft">
                  <el-icon><Shop /></el-icon>
                  <template #title>
                    交易广场
                  </template>
                </el-menu-item>
                <el-menu-item index="2-2">
                  <el-icon><Medal /></el-icon>
                  <template #title>
                    牛逼卖家
                  </template>
                </el-menu-item>
                <el-menu-item index="/index/nft/mint">
                  <el-icon><Trophy /></el-icon>
                  <template #title>
                    铸币板块
                  </template>
                </el-menu-item>
                <el-menu-item index="2-4">
                  <el-icon><Document /></el-icon>
                  <template #title>
                    销售记录
                  </template>
                </el-menu-item>
              </el-sub-menu>
<!--              链游-->
              <el-sub-menu index="3">
                <template #title>
                  <el-icon><SwitchFilled /></el-icon>
                  NFT竞技游戏
                  <el-tag style="margin-left: 10px;margin-right: 5px" size="small">待开发</el-tag>
                </template>
                <el-menu-item index="3-1">
                  <el-icon><MagicStick /></el-icon>
                  <template #title>
                    匹配对战
                  </template>
                </el-menu-item>
                <el-menu-item index="3-2">
                  <el-icon><Star /></el-icon>
                  <template #title>
                    卡牌一览
                  </template>
                </el-menu-item>
                <el-menu-item index="3-3">
                  <el-icon><More /></el-icon>
                  <template #title>
                    其他玩法
                  </template>
                </el-menu-item>
                </el-sub-menu>
<!--              设置-->
              <el-sub-menu index="4">
                <template #title>
                  <el-icon><User /></el-icon>
                  账户信息管理
                </template>
                <el-menu-item index="/index/nft/account">
                  <el-icon><Postcard /></el-icon>
                  <template #title>
                    我的NFT
                  </template>
                </el-menu-item>
                <el-menu-item index="4-2">
                  <el-icon><Money /></el-icon>
                  <template #title>
                    我的资产
                  </template>
                </el-menu-item>
                <el-menu-item index="/index/privacy-setting">
                  <el-icon><Setting /></el-icon>
                  <template #title>
                    账号安全
                  </template>
                </el-menu-item>
                <el-menu-item index="/index/user-setting">
                  <el-icon><More /></el-icon>
                  <template #title>
                    其他设置
                  </template>
                </el-menu-item>

              </el-sub-menu>
            </el-menu>
          </el-scrollbar>
        </el-aside>


        <el-main class="main-content-page" style="padding: 0">
          <el-scrollbar style="height: calc(100vh - 55px)">
            <router-view  v-slot="{ Component }">
              <transition name="el-zoom-in-center" mode="out-in">
                <component :is="Component" style="height: 100%"/>
              </transition>
            </router-view>
          </el-scrollbar>

        </el-main>
      </el-container>
    </el-container>
  </div>
</template>




<style lang="less" scoped>
.notification-item {
  transition: .3s;
  &:hover {
    cursor: pointer;
    opacity: 0.7;
  }
}

.notification {
  font-size: 22px;
  line-height: 14px;
  text-align: center;
  transition: color .3s;

  &:hover {
    color: grey;
    cursor: pointer;
  }
}

.main-content-page {
  padding: 0;
  background-color: #f7f8fa;
}

.dark .main-content-page {
  background-color: #212225;
}

.main-content {
  height: 100vh;
  width: 100vw;
}

.main-content-header {
  border-bottom: solid 1px var(--el-border-color);
  height: 55px;
  display: flex;
  align-items: center;
  box-sizing: border-box;

  .logo {
    height: 32px;
  }

  .user-info {
    display: flex;
    justify-content: flex-end;
    align-items: center;

    .el-avatar:hover {
      cursor: pointer;
    }

    .profile {
      text-align: right;
      margin-right: 20px;

      :first-child {
        font-size: 18px;
        font-weight: bold;
        line-height: 20px;
      }

      :last-child {
        font-size: 10px;
        color: grey;
      }
    }
  }
}
</style>
